package com.sch.wanandroid.adapter

import android.widget.ImageView
import androidx.core.text.HtmlCompat
import coil.load
import coil.size.Scale
import coil.transform.RoundedCornersTransformation
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.sch.wanandroid.R
import com.sch.wanandroid.entity.ArticleBean

/**
 * Created by Sch.
 * Date: 2020/12/14
 * description:
 */
class ArticleAdapter : BaseMultiItemQuickAdapter<ArticleBean, BaseViewHolder>() {
    init {
        addItemType(1, R.layout.item_article)
        addItemType(2, R.layout.item_project)
    }

    override fun convert(holder: BaseViewHolder, item: ArticleBean) {
        when (holder.itemViewType) {
            1 -> {
                if (item.author.isEmpty()) {
                    holder.setText(R.id.tvAuthor, item.shareUser)
                } else {
                    holder.setText(R.id.tvAuthor, item.author)
                }
                holder.setText(R.id.tvTag, if (item.type == 1) "置顶  " else "")
                holder.setText(
                    R.id.tvTitle,
                    HtmlCompat.fromHtml(item.title, HtmlCompat.FROM_HTML_MODE_LEGACY)
                )
                holder.setText(R.id.tvDate, item.niceDate)
                holder.setText(R.id.tvChapterName, item.superChapterName)
                holder.setImageResource(
                    R.id.ivCollect,
                    if (item.collect) R.drawable.ic_heart_red
                    else R.drawable.ic_heart_normal
                )
            }
            2 -> {
                holder.setText(
                    R.id.tvTitle,
                    HtmlCompat.fromHtml(item.title, HtmlCompat.FROM_HTML_MODE_LEGACY)
                )
                holder.setText(R.id.tvDes, item.desc)
                holder.setText(R.id.tvNameData, item.niceDate + "  |  " + item.author)
                holder.getView<ImageView>(R.id.ivTitle).load(item.envelopePic) {
                    transformations(RoundedCornersTransformation(10F))
                }
                holder.setImageResource(
                    R.id.ivCollect,
                    if (item.collect) R.drawable.ic_heart_red
                    else R.drawable.ic_heart_normal
                )
            }
        }
    }
}